package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.RoutingFieldDTO;
import com.example.demo.request.CreateRoutingFieldRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.RoutingFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/routing-fields")

public class RoutingFieldController {

    @Autowired
    private RoutingFieldService routingFieldService;


    @GetMapping
    public ResponseEntity<ApiResponse<List<RoutingFieldDTO>>> getRoutingFields(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {

        List<RoutingFieldDTO> routingFields = routingFieldService.getRoutingFields(page, size);
        long total = routingFieldService.countRoutingFields();
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(routingFields, "请求成功", request.getRequestURI(), extra)
        );
    }


    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<RoutingFieldDTO>> getRoutingField(
             @PathVariable Integer id,
            HttpServletRequest request) {

        RoutingFieldDTO routingField = routingFieldService.getRoutingFieldById(id);
        return ResponseEntity.ok(
                ApiResponse.success(routingField, "查询成功", request.getRequestURI(), null)
        );
    }


    @PostMapping
    public ResponseEntity<ApiResponse<RoutingFieldDTO>> createRoutingField(
            @Valid @RequestBody CreateRoutingFieldRequest request,
            HttpServletRequest requestObj) {

        com.example.demo.entity.RoutingField routingField = routingFieldService.createRoutingField(request);
        RoutingFieldDTO dto = RoutingFieldDTO.fromEntity(routingField);
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "路由字段创建成功", requestObj.getRequestURI())
        );
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<RoutingFieldDTO>> updateRoutingField(
            @PathVariable Integer id,
            @Valid @RequestBody CreateRoutingFieldRequest request,
            HttpServletRequest requestObj) {

        routingFieldService.updateRoutingField(id, request);
        RoutingFieldDTO routingField = routingFieldService.getRoutingFieldById(id);
        return ResponseEntity.ok(
                ApiResponse.success(routingField, "路由字段更新成功", requestObj.getRequestURI(), null)
        );
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteRoutingField(
            @PathVariable Integer id,
            HttpServletRequest requestObj) {

        routingFieldService.deleteRoutingField(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "路由字段删除成功", requestObj.getRequestURI(), null)
        );
    }
    

    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<RoutingFieldDTO>>> searchRoutingFieldsByChineseName(
            @RequestParam String chineseName,
            HttpServletRequest request) {

        List<RoutingFieldDTO> routingFields = routingFieldService.searchRoutingFieldsByChineseName(chineseName);
        return ResponseEntity.ok(
                ApiResponse.success(routingFields, "搜索成功", request.getRequestURI(), null)
        );
    }
}